package com.aramco.fion.service
{
	import com.aramco.fion.model.AppModel;
	import com.aramco.fion.model.FlexConstants;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.AsyncToken;
	import mx.rpc.http.HTTPService;

	public class LoadQueryDelegate implements ILoadQueryDelegate
	{
		public function LoadQueryDelegate()
		{
		}
		
		var myService:HTTPService;
		
		[Inject]
		public var model:AppModel;
		

//	Selection Metadata
		public function loadSelection(id:String):AsyncToken
		{
			myService = new HTTPService();
			myService.url = model.completeURL + FlexConstants.BSP_02_URL + "?rep_id=" + id;
		//	myService.url = "BSP02_TRS.xml";
			myService.method = "GET";
			myService.resultFormat = "e4x";
			myService.headers["Pragma"] = "no-cache"; 
			myService.headers["Cache-Control"] = "no-cache";
			var token: AsyncToken = (myService as HTTPService).send();
			//prepareHandlers(token, new Callbacks(get_Result, get_Fault));
			return token;
		}
		
		
		public function loadSelectionByQueryName(queryName:String, selections:ArrayCollection=null) : AsyncToken
		{
			if(queryName != null)
			{
				//	var myURL:VariablesToURLHelper = new VariablesToURLHelper(_commonModel.selections);
				//	var mycurrentState:CurrentStateVo = Common_ModelLocator.getInstance().currentState
				myService = new HTTPService();
				myService.url = model.completeURL + FlexConstants.BSP_04_URL + '?query=' + 'Report%2F'+ queryName + FlexConstants.BSP_04_URL_FLAGS;
				myService.method = "GET";
				myService.resultFormat = "e4x";
				myService.showBusyCursor = true;
				myService.headers["Pragma"] = "no-cache"; 
				myService.headers["Cache-Control"] = "no-cache";
				/*if(selections != null)
				{
				if(selections.length > 0)
				{
				myService.url = myService.url + (new VariablesToURLHelper(selections)).getEncodedVariables();	
				}
				}*/
				//myService.url+= formatted_flag01;
				var token: AsyncToken = (myService as HTTPService).send();
			}
			return token;
		}
		
//	Load XML Report
		public function loadReportByQueryName(queryName:String, selections:ArrayCollection=null) : AsyncToken
		{
			var token: AsyncToken;
			if(queryName != null)
			{
				//	var myURL:VariablesToURLHelper = new VariablesToURLHelper(_commonModel.selections);
				//	var mycurrentState:CurrentStateVo = Common_ModelLocator.getInstance().currentState
				myService = new HTTPService();
				myService.url = model.completeURL + FlexConstants.BSP_04_URL + '?query=' + 'Report%2F'+ queryName + FlexConstants.BSP_04_URL_FLAGS;
				myService.method = "GET";
				myService.resultFormat = "e4x";
				myService.showBusyCursor = true;
				myService.headers["Pragma"] = "no-cache"; 
				myService.headers["Cache-Control"] = "no-cache";
				/*if(selections != null)
				{
				if(selections.length > 0)
				{
				myService.url = myService.url + (new VariablesToURLHelper(selections)).getEncodedVariables();	
				}
				}*/
				//myService.url+= formatted_flag01;
				token  = (myService as HTTPService).send();
			}
			return token;
		}
		
//	Load XML App Delegate
		public function loadAppXml(url:String):AsyncToken
		{
			myService = new HTTPService();
			//myService.url = _commonModel.completeURL + FlexConstants.BSP_04_URL + '?query=' + 'Report%2F'+ targetState.queryName + FlexConstants.BSP_04_URL_FLAGS;
			myService.url = url;
			myService.method = "GET";
			myService.resultFormat = "e4x";
			myService.showBusyCursor = true;
			myService.headers["Pragma"] = "no-cache"; 
			myService.headers["Cache-Control"] = "no-cache";
			return myService.send();	
		}
		
//	Load F4 Delefate
		public function loadF4(variableName:String):AsyncToken
		{
			myService = new HTTPService();
			//myService.url = _commonModel.completeURL + FlexConstants.BSP_04_URL + '?query=' + 'Report%2F'+ targetState.queryName + FlexConstants.BSP_04_URL_FLAGS;
			myService.url = model.completeURL + FlexConstants.BSP_03_URL + '?var_name=' + variableName + '&page_flag=X' + '&read_mode=C';
			//myService.url = model.completeURL + FlexConstants.BSP_03_URL + '?iobjnm=ZHTR_EMP__H1PSG&infocube=ZMTRCP01';
			//myService.url = "BSP03_TRS.xml";
			myService.method = "GET";
			myService.resultFormat = "e4x";
			myService.showBusyCursor = true;
			myService.headers["Pragma"] = "no-cache"; 
			myService.headers["Cache-Control"] = "no-cache";
			return myService.send();
		}
		
		public function loadF4TRS(parms:Object):AsyncToken
		{
			myService = new HTTPService();
			//myService.url = _commonModel.completeURL + FlexConstants.BSP_04_URL + '?query=' + 'Report%2F'+ targetState.queryName + FlexConstants.BSP_04_URL_FLAGS;
			myService.url = model.completeURL + FlexConstants.BSP_03_URL  ;
			//myService.url = model.completeURL + FlexConstants.BSP_03_URL + '?iobjnm=ZHTR_EMP__H1PSG&infocube=ZMTRCP01';
			//myService.url = "BSP03_TRS.xml";
			myService.method = "GET";
			myService.resultFormat = "e4x";
			myService.request = parms;
			myService.showBusyCursor = true;
			myService.headers["Pragma"] = "no-cache"; 
			myService.headers["Cache-Control"] = "no-cache";
			return myService.send();
		}
		
		public function loadF4CostCenter(parms:Object):AsyncToken
		{
			myService = new HTTPService();
			//myService.url = _commonModel.completeURL + FlexConstants.BSP_04_URL + '?query=' + 'Report%2F'+ targetState.queryName + FlexConstants.BSP_04_URL_FLAGS;
			myService.url = model.completeURL + FlexConstants.BSP_05_URL  ;
			//myService.url = model.completeURL + FlexConstants.BSP_03_URL + '?iobjnm=ZHTR_EMP__H1PSG&infocube=ZMTRCP01';
			//myService.url = "BSP03_TRS.xml";
			myService.method = "GET";
			myService.resultFormat = "e4x";
			myService.request = parms;
			myService.showBusyCursor = true;
			myService.headers["Pragma"] = "no-cache"; 
			myService.headers["Cache-Control"] = "no-cache";
			return myService.send();
		}
		
		public function loadF4Org(parms:Object):AsyncToken
		{
			myService = new HTTPService();
			//myService.url = _commonModel.completeURL + FlexConstants.BSP_04_URL + '?query=' + 'Report%2F'+ targetState.queryName + FlexConstants.BSP_04_URL_FLAGS;
			myService.url = model.completeURL + FlexConstants.BSP_06_URL  ;
			//myService.url = model.completeURL + FlexConstants.BSP_03_URL + '?iobjnm=ZHTR_EMP__H1PSG&infocube=ZMTRCP01';
			//myService.url = "BSP03_TRS.xml";
			myService.method = "GET";
			myService.resultFormat = "e4x";
			myService.request = parms;
			myService.showBusyCursor = true;
			myService.headers["Pragma"] = "no-cache"; 
			myService.headers["Cache-Control"] = "no-cache";
			return myService.send();
		}
		
		//	Load XML App Delegate
		public function loadWelcomePage(url:String):AsyncToken
		{
			myService = new HTTPService();
			//myService.url = _commonModel.completeURL + FlexConstants.BSP_04_URL + '?query=' + 'Report%2F'+ targetState.queryName + FlexConstants.BSP_04_URL_FLAGS;
			myService.url = url;
			myService.method = "GET";
			myService.resultFormat = "e4x";
			myService.showBusyCursor = true;
			//myService.headers["Pragma"] = "no-cache"; 
			//	myService.headers["Cache-Control"] = "no-cache";
			return myService.send();	
		}
	}
}